//
//  ZNYAxisView.m
//  ZNChart
//
//  Created by Yim on 16/9/24.
//  Copyright © 2016年 LQ. All rights reserved.
//

#import "ZNYAxisView.h"
#import "UIView+LQExtension.h"
#import "ZNDraw.h"

@interface ZNYAxisView ()
@property (strong, nonatomic) UIView *separate;
@end

@implementation ZNYAxisView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        // 垂直坐标轴
        UIView *separate = [[UIView alloc] init];
        self.separate = separate;
        [self addSubview:separate];
        
        self.textFont = [UIFont systemFontOfSize:12];
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
}



- (void) draw
{
     self.backgroundColor = [UIColor clearColor];
    
    for (UILabel *label in self.subviews) {
        if ([label isKindOfClass:[UILabel class]]) {
            
            [label removeFromSuperview];
        }
    }
    
    CGFloat textHeight = [@"x" sizeWithAttributes:@{NSFontAttributeName : self.textFont}].height;
    
    // 标题label
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentLeft;// UITextAlignmentRight;
    label.font = self.textFont;
    label.textColor = self.textColor;
    label.text = self.yAxisTitle;
    label.x = 5;
    label.y = 5;
    label.width = self.width + 50;
    label.height = textHeight;
    [self addSubview:label];
    
    CGFloat yAxisHeight = self.height - textHeight - _xAxisTextGap;
    
    // 计算坐标轴的位置以及大小
    self.separate.backgroundColor = self.axisColor;
    self.separate.x = self.width - 1;
    self.separate.width = 1;
    self.separate.y = 0;
    self.separate.height = yAxisHeight;
    
    self.separate.backgroundColor = self.axisColor;
    
    CGFloat allLabelHeight = yAxisHeight - self.topMargin;
    
    CGFloat labelGap = (allLabelHeight - (_yAxisNumber) * textHeight) / _yAxisNumber;
    
    
    for (int i = 0; i <= _yAxisNumber; i ++) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentRight;// UITextAlignmentRight;
        label.font = self.textFont;
        label.textColor = self.textColor;
        label.x = 0;
        label.y = yAxisHeight - (textHeight + labelGap) * i - textHeight/2;
        label.width = self.width - 1 - self.yAxisTextGap;
        label.height = textHeight;
        
        CGFloat yAxisValue = self.yAxisMaxValue / (self.yAxisNumber);
        if (self.isPercent) {
            label.text = [NSString stringWithFormat:@"%.0f%%", yAxisValue * i];
        }else{
            label.text = [NSString stringWithFormat:@"%.0f", yAxisValue * i];
        }
        
        [self addSubview:label];
    }
}

- (void)drawRect:(CGRect)rect {
    
    [self draw];
    
    //    画箭头
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(self.width - 4.5, 5)];
    
    [path addLineToPoint:CGPointMake(self.width - 0.5, 0)];
    [path addLineToPoint:CGPointMake(self.width + 3.5, 5)];
    
//    [path stroke];
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = path.CGPath;
    layer.strokeColor = self.axisColor.CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = 1.0;
    [self.layer addSublayer:layer];
}




@end
